/*
 * session.h
 *
 *  Created on: Aug 1, 2013
 *      Author: killerg
 */

#ifndef SESSION_H_
#define SESSION_H_

#include "client_stat.h"
struct tran_s;
struct player_t;


typedef std::shared_ptr<player_t> sp_player_t;


enum EServerStage {
    ESInvalid    =  0,
    ESConnedted  =  1,
    ESNeedCreate =  2,
    ESCreating   =  3,
    ESCreated    =  4,
    ESLoading    =  5,
    ESLoaded     =  6,
};

const int ESSTAGENUM = ESLoaded+1;

extern const char* ServerStageName[];

#include "share/session_mgr.h"
#include "share/cs_msg.pb.h"

struct client_session_t {
    tran_s* _trans;
    int64_t _sessid;
    double _idle_timer;

    client_session_t(int64_t sessid_, tran_s* trans):
        _trans(trans), online_time(0), playerid(-1), player(NULL), idle_timer(180.0f), stage(ESInvalid) {
        _sessid = sessid_;
        _idle_timer = 20.0f;
    }

    void update();

    void on_connect();
    void on_disconnect();

    bool send_msg(cs::SMsg* &msg);
    void send_notify(const char* fmt, ...);
    int64_t playerid;
    EServerStage stage;
    sp_player_t player;
    double online_time;
    double idle_timer;
    string account;

    client_stat_t stat;
};

int32_t pick_one(int32_t setid, int32_t* num = NULL, int32_t* idx = NULL);
bool check_item_by_type(sp_player_t player, int32_t type, int num, int32_t* protoid = NULL);
bool check_item_by_serial(sp_player_t player, int32_t serial, int32_t type, int num);

struct clientsess_mgr_t : public svr_session_t<client_session_t, cs::CMsg, cs::CMsgID_ARRAYSIZE, cs::SMsg, cs::SMsgID_ARRAYSIZE> {
    typedef svr_session_t<client_session_t, cs::CMsg, cs::CMsgID_ARRAYSIZE, cs::SMsg, cs::SMsgID_ARRAYSIZE> base;

    bool init();
};


#endif /* SESSION_H_ */
